﻿using HDInsightStudio.Properties;
using Microsoft.Hadoop.Client;
using Microsoft.Hadoop.WebHDFS;
using Microsoft.Hadoop.WebHDFS.Adapters;
using Microsoft.WindowsAzure.Management.HDInsight;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using ViewMaker;
using ViewMaker.Core;
using ViewMaker.Core.Controls;

namespace HDInsightStudio
{
    [View(typeof(HdInsightConfigView))]
    public class HdInsightConfig : ViewModelBase
    {
        public static readonly HdInsightConfig Instance = new HdInsightConfig();

        public string ClusterName { get; set; }
        public string AsvAccount { get; set; }
        public string AsvKey { get; set; }
        public string AsvContainer { get; set; }
        public string HadoopUser { get; set; }
        public string SubscriptionID { get; set; }
        public string CertFriendlyName { get; set; }

        public IJobSubmissionClient JobClient { get; set; }
        public WebHDFSClient HdfsClient { get; set; }

        public HdInsightConfig()
        {
            ClusterName = Settings.Default.HDInsightClusterName;
            AsvAccount = Settings.Default.AsvAccount;
            AsvKey = Settings.Default.AsvKey;
            AsvContainer = Settings.Default.AsvContainer;
            HadoopUser = Settings.Default.HadoopUser;
            SubscriptionID = Settings.Default.SubscriptionID;
            CertFriendlyName = Settings.Default.CertFriendlyName;

        }

        public ICommand ConnectCommand
        {
            get 
            { 
                return CreateCommand(() => {
                    Setup();
                    var m = ViewUtil.BuildView(new HdfsStudio());
                    m.Show();
                    ExecuteViewCommand(ViewCommands.Close, null);
                    Save();
                }); 
            }
        }

        public void Save()
        {
            Settings.Default.HDInsightClusterName = ClusterName;
            Settings.Default.AsvAccount = AsvAccount;
            Settings.Default.AsvKey = AsvKey;
            Settings.Default.AsvContainer = AsvContainer;
            Settings.Default.HadoopUser = HadoopUser;
            Settings.Default.SubscriptionID = SubscriptionID;
            Settings.Default.CertFriendlyName = CertFriendlyName;
            Settings.Default.Save();
        }

        public void Setup()
        {
            string subscriptionID = SubscriptionID;
            string certFriendlyName = CertFriendlyName;
            string clusterName = ClusterName;

            // Get the certificate object from certificate store using the friendly name to identify it
            X509Store store = new X509Store();
            store.Open(OpenFlags.ReadOnly);
            X509Certificate2 cert = store.Certificates.Cast<X509Certificate2>().First(item => item.FriendlyName == certFriendlyName);
            JobSubmissionCertificateCredential creds = new JobSubmissionCertificateCredential(new Guid(subscriptionID), cert, clusterName);

            // Create a hadoop client to connect to HDInsight
            JobClient = JobSubmissionClientFactory.Connect(creds);

            var storageAdapter = new BlobStorageAdapter(AsvAccount, AsvKey, AsvContainer, true);
            HdfsClient = new WebHDFSClient(HadoopUser, storageAdapter);
        }

        public ICommand CloseCommand
        {
            get { return CreateViewCommand(ViewCommands.Close); }
        }
    }
}
